package cn.harmonyoslearn.navrouter.utils;
import org.intellij.lang.annotations.Language;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonElement;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Stack;

/**
 * Json处理模块
 */
public class JsonUtil {
    /**
     * 修正JSON字符串格式
     * @param jsonStr Json字符串
     * @return 修正后的JSON字符串格式
     */
    public static String rectify(String jsonStr) {
        // 去除\n
        jsonStr = jsonStr.replace("\n", "");
        jsonStr = jsonStr.replace("\r", "");
        jsonStr = jsonStr.replace(" ", "");

        StringBuilder contentStr = new StringBuilder();
        char lastChar = ' ';
        for (char ch : jsonStr.toCharArray()) {
            if ((ch == '}' || ch == ']') && lastChar == ',') {
                contentStr.delete(contentStr.length() - 1, contentStr.length());
            }
            contentStr.append(ch);
            lastChar = ch;
        }
        return contentStr.toString();
    }

    /**
     * 将JSON字符串转化为可操作的JsonObject
     * @param str Json字符串
     * @return JsonObject数据
     */
    public static JsonObject string2JsonObject(String str) {
        @Language(value = "json", prefix = "", suffix = "") String content = JsonUtil.rectify(str);
        return (JsonObject) new Gson().fromJson(content, JsonElement.class);
    }

    /**
     * 将JsonObject转化为Map类型的数据
     * @param obj JsonObject数据
     * @return Map<String, JsonElement>
     */
    public static Map<String, JsonElement> jsonObject2Map(JsonObject obj) {
        Map<String, JsonElement> map = new HashMap<>();
        Set<String> keys = obj.keySet();
        for (String key : keys) {
            map.put(key, obj.get(key));
        }
        return map;
    }

    /**
     * 将Json字符串转化为map数据
     * @param str Json字符串
     * @return Map<String, JsonElement>
     */
    public static Map<String, JsonElement> string2Map(String str) {
        JsonObject obj = string2JsonObject(rectify(str));
        return jsonObject2Map(obj);
    }

    /**
     * 格式化JSON字符串
     *
     * @param jsonStr 未格式化的JSON字符串
     * @return 格式化的JSON字符串
     */
    public static String formatJson(String jsonStr) {
        StringBuilder result = new StringBuilder();
        Stack<Character> stack = new Stack<>();
        char last = 0;

        for (int i = 0; i < jsonStr.length(); i++) {
            char ch = jsonStr.charAt(i);

            // 开始于一个结构
            if (ch == '{' || ch == '[') {
                result.append(ch);
                result.append("\n");
                stack.push(ch);
                for (int j = 0; j < stack.size(); j++) {
                    result.append("  ");
                }
                last = ch;
            } else if (ch == '}' || ch == ']') {
                result.append("\n");
                stack.pop();
                for (int j = 0; j < stack.size(); j++) {
                    result.append("  ");
                }
                result.append(ch);
                last = ch;
            } else if (ch == ',') {
                result.append(ch);
                if (last != '{' && last != '[' && last != ',') {
                    result.append("\n");
                    for (int j = 0; j < stack.size(); j++) {
                        result.append("  ");
                    }
                }
                last = ch;
            } else {
                result.append(ch);
                last = ch;
            }
        }

        return result.toString();
    }

    public static String jsonObject2String(JsonElement jsonElement) {
        String string = jsonElement.toString();
        return string.replace("\"", "");
    }
}
